package com.dl698.axdr;

import com.dl698.asn1.axdr.AxdrType;
import com.dl698.util.ReverseByteArrayInputStream;
import com.dl698.util.ReverseByteArrayOutputStream;

/**
 * AxdrBoolean类
 * 实现布尔值数据的编解码
 * 
 * @author wind
 * @version 1.1.1
 */
public class AxdrBoolean implements AxdrType {
    private boolean value = false;

    public AxdrBoolean() {
    }

    public AxdrBoolean(boolean value) {
        this.value = value;
    }

    public boolean getValue() {
        return value;
    }

    public void setValue(boolean value) {
        this.value = value;
    }

    @Override
    public int decode(ReverseByteArrayInputStream input) {
        // 解码1个字节的布尔值
        int b = input.read() & 0xFF;
        this.value = (b != 0);
        return 1;
    }

    @Override
    public int encode(ReverseByteArrayOutputStream output) {
        // 编码1个字节的布尔值
        try {
            output.write(this.value ? 1 : 0);
        } catch (java.io.IOException e) {
            throw new RuntimeException(e);
        }
        return 1;
    }

    @Override
    public String toString() {
        return String.valueOf(this.value);
    }
}